import QtQuick 2.0

Item {
    id: unit

    property real healthy: 1   // 生命
    property real damage: 1     // 伤害

    property real _MAX_HEALTHY: 10  // 最大血量

    property real moveSpeed: 0.1
    property real iid: 0
    property color lifeColor: Qt.rgba(0,0,0,1)

    signal die()
    signal byHit(real v)

    function hitSelf(value)
    {
        byHit(value)
        unit.healthy -= value
        if(unit.healthy <= 0) {

            unit.healthy = 0

            // die
            die()
        }
    }

    onByHit: {
        // 血
        var obj = Qt.createComponent("TextDamage.qml").createObject(unit)
        obj.text = "-" + v
        obj.x = unit.width/2 - obj.width/2
        obj.y = obj.sy = unit.height/2
        obj.up()

        // 闪
    }

    // 生命条
    Rectangle{
        id: rectBg
        color: "black"
        width: 30
        height: 5
        anchors.horizontalCenter: parent.horizontalCenter
        y: -15

        Rectangle{
            color: lifeColor
            width: rectBg.width * healthy / _MAX_HEALTHY
            height: rectBg.height
        }
    }

}
